﻿using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NTmdb.TestProject
{
    /// <summary>
    ///     This is a test class for TmdbGuestSessionTest and is intended
    ///     to contain all TmdbGuestSessionTest Unit Tests
    /// </summary>
    [TestClass]
    public class TmdbGuestSessionTest
    {
        /// <summary>
        ///     A test for TmdbGuestSession Constructor
        /// </summary>
        [TestMethod]
        public void TmdbGuestSessionConstructorTest()
        {
            var target = new TmdbGuestSession();
            Assert.IsNotNull( target );
        }

        /// <summary>
        ///     A test for GetExpireDate
        /// </summary>
        [TestMethod]
        public void GetExpireDateTest()
        {
            var target = new TmdbGuestSession();
            target.ExpiresAtString = DateTime.Now.ToString( "yyyy-MM-dd" );
            const string dateTimePattern = "yyyy-MM-dd";
            target.ExpiresAtString = "2010-09-10";
            var expected = DateTime.ParseExact( target.ExpiresAtString, dateTimePattern, CultureInfo.InvariantCulture );
            var actual = target.GetExpireDate( dateTimePattern );
            Assert.AreEqual( expected.Year, actual.Value.Year );
        }

        /// <summary>
        ///     A test for GetIsExpired
        /// </summary>
        [TestMethod]
        public void GetIsExpiredTest()
        {
            var target = new TmdbGuestSession();
            target.ExpiresAtString = DateTime.Now.ToString( "yyyy-MM-dd" );
            const string dateTimePattern = "yyyy-MM-dd";
            target.ExpiresAtString = "2010-09-10";
            var expected = DateTime.ParseExact( target.ExpiresAtString, dateTimePattern, CultureInfo.InvariantCulture );
            var actual = target.GetExpireDate( dateTimePattern );
            Assert.AreEqual( expected.Year, actual.Value.Year );
        }

        /// <summary>
        ///     A test for ExpiresAtString
        /// </summary>
        [TestMethod]
        public void ExpiresAtStringTest()
        {
            var target = new TmdbGuestSession();
            target.ExpiresAtString = DateTime.Now.ToString( "yyyy-MM-dd" );
            const string dateTimePattern = "yyyy-MM-dd";
            target.ExpiresAtString = "2010-09-10";
            var expected = DateTime.ParseExact( target.ExpiresAtString, dateTimePattern, CultureInfo.InvariantCulture );
            var actual = target.GetExpireDate( dateTimePattern );
            Assert.AreEqual( expected.Year, actual.Value.Year );
        }

        /// <summary>
        ///     A test for GuestSessionId
        /// </summary>
        [TestMethod]
        public void GuestSessionIdTest()
        {
            var target = new TmdbGuestSession();
            string expected = Guid.NewGuid().ToString();
            target.GuestSessionId = expected;
            string actual = target.GuestSessionId;
            Assert.AreEqual( expected, actual );
        }

        /// <summary>
        ///     A test for Success
        /// </summary>
        [TestMethod]
        public void SuccessTest()
        {
            var target = new TmdbGuestSession();
            bool expected = new Random().Next( 0, Int32.MaxValue ) % 2 == 0;
            target.Success = expected;
            bool actual = target.Success;
            Assert.AreEqual( expected, actual );
        }

        /// <summary>
        ///     A test for Serialize & Deserialize
        /// </summary>
        [TestMethod]
        [DeploymentItem(@"..\..\TestData\guestSession.json.txt")]
        public async Task SerializeDeserializeTest()
        {
            var target = new TmdbGuestSession();
            var jsonContent = File.ReadAllText("guestSession.json.txt");
            var actual = await target.DeserializeJson<TmdbGuestSession>(jsonContent);

            Assert.AreEqual("2013-07-20 14:13:23 UTC", actual.ExpiresAtString);
            Assert.AreEqual(true, actual.Success);
            Assert.AreEqual("8ba0d6d19dcc4e5dd1e114d7116dd454", actual.GuestSessionId);
        }
    }
}